
#import "FAMMineAddWeightView.h"
@interface FAMMineAddWeightView()
@property (weak, nonatomic) IBOutlet UITextField *FAMTextField;
@property (weak, nonatomic) IBOutlet UIButton *FAMLbsButton;
@property (weak, nonatomic) IBOutlet UIButton *FAMKgButton;

@end
@implementation FAMMineAddWeightView
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"FAMMineAddWeightView" owner:self options:nil];
        self = array.firstObject;
        [self FAM_showUi];
    }
    return self;
}
- (void)FAM_showUi{
    self.FAMTextField.layer.borderWidth = 1;
    self.FAMTextField.layer.borderColor = kCustom0xColor(0xBABABA, 1.0).CGColor;
}
- (IBAction)FAM_closeButtonClick:(UIButton *)sender {
    [self removeFromSuperview];
}
- (void)layoutSubviews{
    [super layoutSubviews];
    self.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
}
- (IBAction)FAM_lbsButttonClick:(UIButton *)sender {
    [sender setSelected:YES];
    sender.backgroundColor = kBlackColor;
    [sender setTitleColor:kWhiteColor forState:UIControlStateNormal];
    self.FAMKgButton.backgroundColor = kClearColor;
    [self.FAMKgButton setTitleColor:kBlackColor forState:UIControlStateNormal];
    [self.FAMKgButton setSelected:NO];
}
- (IBAction)FAM_kgButtonClick:(UIButton *)sender {
    [sender setSelected:YES];
    sender.backgroundColor = kBlackColor;
    [sender setTitleColor:kWhiteColor forState:UIControlStateNormal];
    self.FAMLbsButton.backgroundColor = kClearColor;
    [self.FAMLbsButton setTitleColor:kBlackColor forState:UIControlStateNormal];
    [self.FAMLbsButton setSelected:NO];
}

- (IBAction)FAMConfirmButtonClick:(UIButton *)sender {
    if ([[NSString nullToString:self.FAMTextField.text] isEqualToString:@""]) {
        [UIWindow.currentViewController showHint:@"Please enter your weight"];
        return;
    }
    NSString *weight;
    if (self.FAMKgButton.isSelected) {
        weight = self.FAMTextField.text;
    }else{
        CGFloat tempWeight = self.FAMTextField.text.floatValue * lbs;
        weight = [NSString stringWithFormat:@"%0.1f",tempWeight];
    }
    WeakSelf(self);
    [UIWindow.currentViewController showHudInKeyWindowWithHint:nil];
    [FAMHttpTool FAM_PutFoodmateUserdataWithParameters:[NSMutableDictionary dictionaryWithDictionary:@{@"data":@{@"weight":weight}}] success:^(FAMFoodmateUserdataModel * _Nonnull model, NSString * _Nonnull msg) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [UIWindow.currentViewController hideHudKeyWindow];
            if ([FAMUserManger shareInstance].userModel.weights.count > 0 && [((FAMWeightsModel *)[FAMUserManger shareInstance].userModel.weights.lastObject).fmt_date isEqualToString:[FAMTool FAM_getYearWithMonthWithDay]]) {
                ((FAMWeightsModel *)[FAMUserManger shareInstance].userModel.weights.lastObject).weight = weight;
            }else{
                FAMWeightsModel *model = [[FAMWeightsModel alloc]init];
                model.fmt_date = [FAMTool FAM_getYearWithMonthWithDay];
                model.weight = weight;
                [[FAMUserManger shareInstance].userModel.weights addObject:model];
            }
            [FAMUserManger shareInstance].userModel.weight = weight;
            if (weakSelf.block) {
                weakSelf.block();
            }
            [weakSelf removeFromSuperview];
        });
    } failure:^(NSString * _Nonnull errorInfo) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [UIWindow.currentViewController hideHudKeyWindow];
            [UIWindow.currentViewController showHint:errorInfo];
        });
    }];
}

@end
